#!/usr/bin/env python3
# encoding=utf8

import requests
from bs4 import BeautifulSoup
import base64

url = 'https://hao.su/531/'
headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.113 Safari/537.36'}
r = requests.get(url, headers = headers)

soup = BeautifulSoup(r.text, 'html.parser')
content = soup.find('div', class_='post-content')
#print(content)
tables = content.find_all('table')
#print(tables)

def packItem(title, desc, link):
    link = link[:link.find('?')]
    uri = 'https://home.hanxi.info:2112/icon?size=80..120..200&url=' + link
    response = requests.get(uri)
    logo = None
    if response.status_code == 200:
        content_type = response.headers["content-type"]
        encoded_body = base64.b64encode(response.content)
        logo = "data:{};base64,{}".format(content_type, encoded_body.decode())

    flogo = title[0]
    item = {
        'title': title,
        'description': desc,
        'url': link,
        'flogo': flogo
    }

    if logo:
        item['logo'] = logo

    return item


def tojson(tables):
    result = []
    for table in tables:
        title = table.previous_sibling.string
        print(title)
        col_len = len(table('tr')[0])
        if col_len == 2:
            links = col2json(table)
        if col_len == 3:
            links = col3json(table)
        if col_len == 4:
            links = col4json(table)
        result.append({
            'name': title,
            'icon': 'fa-folder-o',
            'links': links
        })
    return result

def col2json(table):
    arr = []
    for row in table('tr'):
        a = row('td')[0]('a')
        if len(a) == 1:
            cell = a[0]
            link = cell.get('href')
            title = cell.text
            desc = row('td')[1].text
            print(title, desc, link, title[0])
            item= packItem(title, desc, link)
            arr.append(item)
    return arr

def col3json(table):
    arr = []
    for row in table('tr'):
        a = row('td')[0]('a')
        if len(a) == 1:
            cell = a[0]
            link = cell.get('href')
            title = cell.text
            desc = row('td')[2].text
            print(title, desc, link, title[0])
            item= packItem(title, desc, link)
            arr.append(item)
    return arr

def col4json(table):
    arr = []
    for row in table('tr'):
        for col in row('td'):
            a = col('a')
            if len(a) == 1:
                cell = a[0]
                link = cell.get('href')
                title = cell.text
                desc = ''
                print(title, desc, link, title[0])
                item= packItem(title, desc, link)
                arr.append(item)
    return arr

result = tojson(tables)
print(result)
import json
j=json.dumps(result, sort_keys=True, indent=4, separators=(',', ': '))

import yaml
with open('_data/webstack.yml', 'wb+') as f:
    #yaml.dump(result, f, allow_unicode=True, encoding='utf-8', default_flow_style=False)
    yaml.dump(json.loads(j), f, allow_unicode=True, encoding='utf-8', default_flow_style=False)

#python -c 'import sys, yaml, json; yaml.dump(json.load(sys.stdin), sys.stdout, default_flow_style=False)' < file.json > file.yaml

